<template>
  <el-dialog
    customClass="elc-dialog"
    :close-on-click-modal="false"
    :title="title"
    width="680px"
    :visible.sync="dialogVisible"
    @close="cancel"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="125px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级菜单：" prop="parentId">
            <treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="类型：" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio label="0">菜单</el-radio>
              <el-radio label="1">按钮</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.type != '1'">
          <el-form-item label="图标：" prop="icon" style="position: relative">
            <el-select
              popper-class="iconList"
              v-model="form.icon"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in iconLists"
                :key="item"
                :label="item"
                :value="item"
              >
                <span>
                  <span :class="item"></span>
                </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入菜单名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序：" prop="menuOrderNum">
            <el-input-number
              v-model="form.orderNum"
              controls-position="right"
              :min="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.type != '1'">
          <el-form-item prop="url">
            <span slot="label">
              <el-tooltip
                content="访问的路由地址，如：`user`，如外网地址需内链访问则以`http(s)://`开头"
                placement="top"
              >
                <i class="el-icon-question"></i>
              </el-tooltip>
              路由地址：
            </span>
            <el-input v-model="form.url" placeholder="请输入路由地址" />
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="form.type != '0'">
          <el-form-item prop="url" label="标识：">
            <el-input v-model="form.url" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
// import IconSelect from "@/components/IconSelect";

export default {
  name: "Menu",
  components: { Treeselect },
  data() {
    return {
      iconLists: [
        "el-icon-upload",
        "el-icon-menu",
        "icon-fangkuai",
        "icon-shu",
        "icon-wenjianjia",
        "el-icon-cpu",
        "el-icon-data-analysis",
        "icon-wenjian",
        "el-icon-s-help",
        "el-icon-s-management",
        "el-icon-s-grid",
        "el-icon-s-opportunity",
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        menuName: undefined,
        visible: undefined,
      },
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级菜单不能为空", trigger: "blur" },
        ],
        type: [{ required: true, message: "类型不能为空！", trigger: "blur" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        url: [{ required: true, message: "路由不能为空", trigger: "blur" }],
      },
      title: "",
      dialogVisible: false,
      form: {
        name: "",
        code: "",
        parentId: "",
        orderNum: "",
        url: "",
        icon: "",
        type: "1",
      },
    };
  },
  created() {},
  methods: {
    show(obj) {
      this.getTreeselect();
      this.title = obj.title;
      this.form = obj.data ? obj.data : this.form;
      this.dialogVisible = true;
    },
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      this.$axios.get(this.$api.setting.menuApi.getAllMenu).then((res) => {
        const { data, code } = res.data;
        if (code == 200) {
          this.menuOptions = [];
          const menu = { id: 0, name: "主类目", children: [] };
          menu.children = data;
          this.menuOptions.push(menu);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.dialogVisible = false;
      this.form = {
        name: "",
        code: "",
        orderNum: "",
        redirect: "",
        icon: "",
        type: "1",
      };
      this.$emit("closeDialog");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.url) {
            Object.assign(this.form, {
              code: this.form.url,
            });
          }
          this.$axios
            .post(this.$api.setting.menuApi.saveOrUpdate, this.form)
            .then((res) => {
              const { code } = res.data;
              if (code == 200) {
                this.reset();
              }
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.iconList {
  width: 500px !important;
}
.iconList .el-select-dropdown__list {
  width: 500px;
  display: flex;
}
.iconList .el-select-dropdown__item {
  display: inline-block !important;
}
</style>
